<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class update_mp extends My_Controller {
	function __construct(){
		parent::__construct();
		$this->load->model('m_admin');
		$this->load->model('mdl_general');
		$this->load->model('mdl_usulan_mp');
		if(!$this->m_admin->isLogin()) {
			redirect('c_home');
		}
	}
	
	public function index() {
		$u_kdprov	= $this->session->userdata("u_kdprov");
		$u_kdkab 	= $this->session->userdata("u_kdkab");
		
		$data['thn']= '-';
		$data['thn']= isset ($_POST['tahun']) ? $this->input->post('tahun') : '-';
		$data['kabkota']= isset ($_POST['kabkota']) ? $this->input->post('kabkota') : '-';
		
		# 1=admin ; 2=user prov; 3=user kab
		$data['typeU'] 		= (empty($u_kdprov) && empty($u_kdkab)) ? '1' : (!empty($u_kdprov) && $u_kdkab == 0 ? '2' : '3');
		
		if ($u_kdkab == 0 && $data['kabkota'] != '-' ) { $u_kdkab= $data['kabkota']; }
		
		$wThn 				= ($data['thn'] != '-') ? " AND tahun_anggaran = '".$data['thn']."' " : "";
		$where				= "WHERE mrpijm.usulan_mp = 1 and kd_wilayah like '".$u_kdprov. ($data['typeU'] == 3 ? '.'.$u_kdkab : ($u_kdkab > 0 ? '.'.$u_kdkab : ''))."%' ".$wThn; 
		$data['rpijm'] 		= $this->mdl_usulan_mp->getRpijm($where, 'rkegiatan.kd_kegiatan');
		$data['rpropinsi'] 	= $this->mdl_general->getWhere(array('kd_propinsi'=>$u_kdprov), 'rpropinsi');
		$data['dpropinsi'] 	= $this->mdl_general->getWhere(array('kd_propinsi'=>$u_kdprov), 'rpropinsi', 1);
		$data['rkabkota'] 	= $this->mdl_general->getWhere(array('kd_propinsi'=>$u_kdprov, 'kd_kabkota'=>$u_kdkab ), 'rkabkota', 1);
		$data['rkegiatan'] 	= $this->mdl_general->getWhere('', 'rkegiatan');
		
		$this->load->view('update_mp/update_mp', $data);
	}
	
	function mrpijm_list ($data) {
		$this->load->view('update_mp/update_mp_list', $data);
	}
	
	function getoutput ($id) {
		$where 	= array('kd_kegiatan' => $id);
		$data	= $this->mdl_general->getWhere($where, 'routput');
		
		echo '<option value="">-- Pilih Output --</option>';
		
		if (!empty($data)) {
			foreach ($data as $row) {
				echo '<option value="'.$row->kd_output.'">'.$row->nama_output.'</option>';
			}
		}
	}
	
	function getkabkota ($id) {
		$where 	= array('kd_propinsi' => $id);
		$data	= $this->mdl_general->getWhere($where, 'rkabkota');
		
		echo '<option value="">-- Pilih Kabkota --</option>';
		
		if (!empty($data)) {
			foreach ($data as $row) {
				echo '<option value="'.$row->kd_kabkota.'">'.$row->nama_kabkota.'</option>';
			}
		}
	}
	
	function kegiatan_listing() {
		$search = "";
		$start 	= 0;
		$rows 	= 10;

		// get search value (if any)
		if (isset($_GET['sSearch']) && $_GET['sSearch'] != "" ) {
			$search = $_GET['sSearch'];
		}

		// limit
		$start	= $this->get_start();
		$rows 	= $this->get_rows();

		// sort
		$sort_dir	= $this->get_sort_dir();
		$sort 		= $this->get_sort();
		
		// run query to get user listing
		$where			= array();
		$user_listing 	= $this->mdl_usulan_mp->getRpijm($where, $start, $rows, $search, $sort, $sort_dir);
		$iFilteredTotal = $user_listing->num_rows();
		$iTotal 		= $this->mdl_usulan_mp->getRpijm($where, 0, '-1', '', '', '')->num_rows();

		# Output
		 $output = array(
			 "sEcho" => intval($_GET['sEcho']),
			 "iTotalRecords" => $iTotal,
			 "iTotalDisplayRecords" => $iTotal,
			 "aaData" => array()
		 );

		// get result after running query and put it in array
		foreach ($user_listing->result() as $row) {
			$record = array();
			
			$record[] = '<img src="'.base_url().'style/images/details_open.png">';
			$record[] = $row->kd_kegiatan;
			$record[] = $row->output;
			$record[] = $row->sub_output;
			$record[] = $row->itemkeg;
			$record[] = $row->kabkota;
			$record[] = $row->lokasirinci;
			$record[] = $row->apbn;
			$record[] = $row->pln;
			$record[] = $row->hibah;
			$record[] = $row->apbd1;
			$record[] = $row->apbd2;
			$record[] = $row->pdam;
			$record[] = $row->swasta;
			$record[] = $row->masy;
			$record[] = $row->dak;
			$record[] = '';
			$output['aaData'][] = $record;
		}
		// format it to JSON, this output will be displayed in datatable
		echo json_encode($output);
	}
	
	function rpijm_listing() {
		$search = "";
		$start = 0;
		$rows = 10;

		// get search value (if any)
		if (isset($_GET['sSearch']) && $_GET['sSearch'] != "" ) {
			$search = $_GET['sSearch'];
		}

		// limit
		$start	= $this->get_start();
		$rows 	= $this->get_rows();

		// sort
		$sort_dir	= $this->get_sort_dir();
		$sort 		= $this->get_sort();
		
		// run query to get user listing
		$user_listing = $this->mdl_usulan_mp->rpijm_listing($start, $rows, $search, $sort, $sort_dir);
		$iFilteredTotal = $user_listing->num_rows();
		$iTotal = $this->mdl_usulan_mp->rpijm_listing(0, '-1', '', '', '')->num_rows();

		# Output
		 $output = array(
			 "sEcho" => intval($_GET['sEcho']),
			 "iTotalRecords" => $iTotal,
			 "iTotalDisplayRecords" => $iTotal,
			 "aaData" => array()
		 );

		// get result after running query and put it in array
		foreach ($user_listing->result() as $row) {
			$record = array();
			
			#$record[] = '<img src="'.base_url().'style/images/details_open.png">';
			$record[] = $row->id_rpijm;
			$record[] = $row->uraian;
			$record[] = $row->kd_wilayah;
			$record[] = $row->detail_lokasi;
			$record[] = $row->volume;
			$record[] = $row->volume;
			$record[] = $row->volume;
			$record[] = $row->volume;
			$record[] = $row->volume;
			$record[] = $row->volume;
			$record[] = $row->volume;
			$record[] = $row->volume;
			$record[] = $row->volume;
			$record[] = $row->volume;
			$record[] = $row->volume;
			$record[] = $row->volume;
			$record[] = $row->volume;
			$output['aaData'][] = $record;
		}
		// format it to JSON, this output will be displayed in datatable
		echo json_encode($output);
	}
	
	function get_start() {
		$start = 0;
		if (isset($_GET['iDisplayStart'])) {
			$start = intval($_GET['iDisplayStart']);

			if ($start < 0)
				$start = 0;
		}

		return $start;
	}

	function get_rows() {
		$rows = 10;
		if (isset($_GET['iDisplayLength'])) {
			$rows = intval($_GET['iDisplayLength']);
			if ($rows < 5 || $rows > 500) {
				$rows = 10;
			}
		}

		return $rows;
	}

	function get_sort_dir() {
		$sort_dir 	= "ASC";
		$sdir 		= isset ($_GET['sSortDir_0']) ?  strip_tags($_GET['sSortDir_0']) : 'ASC';
		if (isset($sdir)) {
			if ($sdir != "ASC" ) {
				$sort_dir = "DESC";
			}
		}

		return $sort_dir;
	}

	function get_sort() {
		$sCol	= isset ($_GET['sSortDir_0']) ?  $_GET['iSortCol_0'] : 0;
		$col 	= 0;
		$cols 	= array("id_rpijm", "uraian", "kd_wilayah", "detail_lokasi", "volume");

		if (isset($sCol)) {
			$col = intval($sCol);
			if ($col < 0 || $col > 4)
				$col = 0;
		}
		$colName = $cols[$col];
		return $colName;
	}	
	
	function getripjm_output($thn,$id) {
		$u_kdprov = $this->session->userdata("u_kdprov");
		$u_kdkab = $this->session->userdata("u_kdkab");
		# 1=admin ; 2=user prov; 3=user kab
		$typeU	= (empty($u_kdprov) && empty($u_kdkab)) ? '1' : (!empty($u_kdprov) && $u_kdkab == 0 ? '2' : '3');
		$wThn 	= ($thn != '-') ? " AND tahun_anggaran = '".$thn."' " : ""; 
		$where 	= "mrpijm.kd_akun like '".$id."%' and mrpijm.usulan_mp = 1 AND kd_wilayah like '".$u_kdprov. ($typeU == 3 ? '.'.$u_kdkab : '')."%' ". $wThn;
		$select	= "rkegiatan.kd_kegiatan, rkegiatan.nama_kegiatan, SUBSTRING( kd_akun, 6, 3 ) as output, routput.nama_output, routput.nama_output as sub_output, '' as itemkeg, '' as kabkota, '' as lokasirinci,  mrpijm.tahun_anggaran, mrpijm.kd_satuan, musulanmp.tahun_usulan_mp";
		$join	= array('rkegiatan', 'routput');
		$groupby= 'rkegiatan.kd_kegiatan, routput.kd_output';
		$orderby= 'kd_kegiatan, output ASC';
		$data	= $this->mdl_usulan_mp->getSubOutputbykdOutput($select, $join, $where, $groupby, $orderby);
		
		if (!empty($data)) {
			echo json_encode($data);
		}
	}
	
	function getripjm_suboutput($thn, $kdKeg, $id) {
		$u_kdprov = $this->session->userdata("u_kdprov");
		$u_kdkab = $this->session->userdata("u_kdkab");
		
		# 1=admin ; 2=user prov; 3=user kab
		$typeU	= (empty($u_kdprov) && empty($u_kdkab)) ? '1' : (!empty($u_kdprov) && $u_kdkab == 0 ? '2' : '3');
		$wThn 	= ($thn != '-') ? " AND tahun_anggaran = '".$thn."' " : "";
		$where 	= "mrpijm.kd_akun like '".$kdKeg.".".$id."%' and mrpijm.usulan_mp = 1 AND kd_wilayah like '".$u_kdprov. ($typeU == 3 ? '.'.$u_kdkab : '')."%' ". $wThn;
		 
		$select	= "rkegiatan.kd_kegiatan, rkegiatan.nama_kegiatan, SUBSTRING( mrpijm.kd_akun, 6, 3 ) as output, routput.nama_output, SUBSTRING( mrpijm.kd_akun, 10, 3 ) as sub_output, rsuboutput.nama_suboutput, '' as itemkeg, '' as kabkota, '' as lokasirinci ,  mrpijm.tahun_anggaran, mrpijm.kd_satuan, musulanmp.tahun_usulan_mp  ";
		$join	= array('rkegiatan', 'routput', 'rsuboutput');
		$groupby= 'rkegiatan.kd_kegiatan, routput.kd_output, rsuboutput.kd_suboutput';
		$orderby= 'kd_kegiatan, output, sub_output  ASC';
		$data	= $this->mdl_usulan_mp->getSubOutputbykdOutput($select, $join, $where, $groupby, $orderby);
		
		if (!empty($data)) {
			echo json_encode($data);
		}
	}
	
	function getripjm_keg ($kdKeg, $kdOut, $id) {
		$where 	= "mrpijm.kd_akun like '".$kdKeg.".".$kdOut.".".$id."%' and mrpijm.usulan_mp = 1";
		$select	= "rkegiatan.kd_kegiatan, rkegiatan.nama_kegiatan, SUBSTRING( mrpijm.kd_akun, 6, 3 ) as output, SUBSTRING( mrpijm.kd_akun, 10, 3 ) as sub_output, SUBSTRING( mrpijm.kd_akun, 14, 3 ) as itemkeg, rkomponen.nama_komponen, '' as kabkota, '' as lokasirinci ,  mrpijm.tahun_anggaran, mrpijm.kd_satuan, musulanmp.tahun_usulan_mp  ";
		$join	= array('rkegiatan', 'routput', 'rsuboutput', 'rkomponen');
		$groupby= 'rkegiatan.kd_kegiatan, routput.kd_output, rsuboutput.kd_suboutput, rkomponen.kd_komponen';
		$orderby= 'kd_kegiatan, output, sub_output, itemkeg  ASC';
		$data	= $this->mdl_usulan_mp->getSubOutputbykdOutput($select, $join, $where, $groupby, $orderby);
		
		if (!empty($data)) {
			echo json_encode($data);
		}
	}
	
	function getripjmData ($thn, $kdKeg, $kdOut, $kdSubOut, $idKabkota) {
		$u_kdprov = $this->session->userdata("u_kdprov");
		$u_kdkab = $this->session->userdata("u_kdkab");
		
		# 1=admin ; 2=user prov; 3=user kab
		$typeU	= (empty($u_kdprov) && empty($u_kdkab)) ? '1' : (!empty($u_kdprov) && $u_kdkab == 0 ? '2' : '3');
		$wThn 	= ($thn != '-') ? " AND tahun_anggaran = '".$thn."' " : "";
		$where 	= "mrpijm.kd_akun like '".$kdKeg.".".$kdOut.".".$kdSubOut."%' and mrpijm.usulan_mp = 1 AND kd_wilayah like '".$u_kdprov. ($typeU == 3 ? '.'.$u_kdkab : '')."%' ". $wThn;
		$select	= "uraian, mrpijm.id_rpijm, mrpijm.status, rkegiatan.kd_kegiatan, rkegiatan.nama_kegiatan, SUBSTRING( mrpijm.kd_akun, 6, 3 ) as output, 
					SUBSTRING( mrpijm.kd_akun, 10, 3 ) as sub_output, '' as itemkeg, concat (rkabkota.kd_propinsi,'.',rkabkota.kd_kabkota) as kabkota, mrpijm.detail_lokasi as lokasirinci ";
		$join	= array('rkegiatan', 'routput', 'rsuboutput', 'rkabkota');
		$groupby= ''; #'rkegiatan.kd_kegiatan, routput.kd_output, rsuboutput.kd_suboutput ';
		$orderby= 'kd_kegiatan, output, sub_output ASC';
		$data	= $this->mdl_usulan_mp->getSubOutputbykdOutput($select, $join, $where, $groupby, $orderby);
		
		if (!empty($data)) {
			echo json_encode($data);
		}
	}
	
	function getripjm_kabkota ($thn, $kdKeg, $kdOut, $id) {
		$u_kdprov = $this->session->userdata("u_kdprov");
		$u_kdkab = $this->session->userdata("u_kdkab");
		
		# 1=admin ; 2=user prov; 3=user kab
		$typeU	= (empty($u_kdprov) && empty($u_kdkab)) ? '1' : (!empty($u_kdprov) && $u_kdkab == 0 ? '2' : '3');
		$wThn 	= ($thn != '-') ? " AND tahun_anggaran = '".$thn."' " : "";
		$where 	= "mrpijm.kd_akun like '".$kdKeg.".".$kdOut.".".$id."%' and mrpijm.usulan_mp = 1 AND kd_wilayah like '".$u_kdprov. ($typeU == 3 ? '.'.$u_kdkab : '')."%' ". $wThn;
		 $select	= "mrpijm.id_rpijm, rkegiatan.kd_kegiatan, rkegiatan.nama_kegiatan, SUBSTRING( mrpijm.kd_akun, 6, 3 ) as output, 
					SUBSTRING( mrpijm.kd_akun, 10, 3 ) as sub_output, '' as itemkeg, rkabkota.kd_kabkota as kabkota, mrpijm.detail_lokasi as lokasirinci ,  mrpijm.tahun_anggaran, mrpijm.kd_satuan, musulanmp.tahun_usulan_mp  ";
		$join	= array('rkegiatan', 'routput', 'rsuboutput', 'rkabkota');
		$groupby= 'rkegiatan.kd_kegiatan, routput.kd_output, rsuboutput.kd_suboutput, rkabkota.kd_kabkota';
		$orderby= 'kd_kegiatan, output, sub_output, kabkota ASC';
		$data	= $this->mdl_usulan_mp->getSubOutputbykdOutput($select, $join, $where, $groupby, $orderby);
		
		if (!empty($data)) {
			echo json_encode($data);
		}
	}
	
	function getripjm_kriteria($id) {
		$data	= $this->mdl_usulan_mp->kriteriabyIdRipjm($id);
		if (!empty($data)) {
			foreach ($data as $row){
				echo '- '.$row->nama_kriteria.'<br>';
			}
			
		}
	}
	
	function update($id) {
	 
		$where 	= "musulanmp.id_usulanmp = $id";
		$select	= "mrpijm.id_rpijm, rkegiatan.kd_kegiatan, rkegiatan.nama_kegiatan, mrpijm.kd_program, SUBSTRING( mrpijm.kd_akun, 6, 3 ) as output, 
					SUBSTRING( mrpijm.kd_akun, 10, 3 ) as sub_output, '' as itemkeg, rkabkota.kd_kabkota as kabkota, mrpijm.detail_lokasi as lokasirinci ,  mrpijm.tahun_anggaran, mrpijm.kd_satuan, musulanmp.tahun_usulan_mp  ";
		$join	= array('rkegiatan', 'routput', 'rsuboutput', 'rkabkota');
		$groupby= 'rkegiatan.kd_kegiatan, routput.kd_output, rsuboutput.kd_suboutput, rkabkota.kd_kabkota';
		$orderby= 'kd_kegiatan, output, sub_output, kabkota ASC';
		$data['results']	= $this->mdl_usulan_mp->getSubOutputbykdOutput2($select, $join, $where, $groupby, $orderby);
		$this->load->view('update_mp/update_mp_update',$data);
	 
	}
	
	
	function process_update()
	{
		$data['id_usulanmp']		= $this->input->post('id_usulanmp'); 		
		$data['id_rpijm'] 			= $this->input->post('id_rpijm'); 
		//$data['kd_program'] 		=  $this->input->post('kd_program');
		//$data['nama_kegiatan'] 		=  $this->input->post('nama_kegiatan');
		//$data['tahun_anggaran']		=  $this->input->post('tahun_anggaran');
		$data['tahun_usulan_mp'] 	=  $this->input->post('tahun_usulan_mp');
		$data['sd_apbn_rpmurni'] 				=  $this->input->post('apbn');
		$data['sd_apbn_pln'] 				=  $this->input->post('pln');
		$data['sd_apbn_hln']				=  $this->input->post('hibah');
		$data['sd_apbd_prop'] 				=  $this->input->post('apbd1');
		$data['sd_apbd_kabkota'] 				=  $this->input->post('apbd2');
		$data['sd_pdam'] 				=  $this->input->post('pdam');
		$data['sd_swasta'] 			=  $this->input->post('swasta');
		$data['sd_masyarakat'] 				=  $this->input->post('masy');
		$data['sd_dak']				=  $this->input->post('dak'); 
		$this->mdl_usulan_mp->process_update($data['id_usulanmp'],$data);
	}
	
	function delete($id)
	{
		 		
		 
		$this->mdl_usulan_mp->process_delete($id);
	}
}
?>
